package com.platform.module.dao;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Collections;

public class CodeGenerator {

    private static final String DB_HOST = "127.0.0.1:3306";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "123456";

    private static final String DB_NAME = "platform_base";
    private static final String DB_TABLE_NAME = "tb_admin_user";


    private static final String OUT_PUT_PATH = "/platform-module-dao/src/main/java";
    private static final String author = "pxj";

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);

        FastAutoGenerator.create("jdbc:mysql://" + DB_HOST + "/" + DB_NAME
                                + "?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true"
                        , USER_NAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .dateType(DateType.ONLY_DATE)
                            .outputDir(projectPath + OUT_PUT_PATH); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.platform") // 设置父包名
                            .moduleName("module") // 设置父包模块名
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath +
                                    OUT_PUT_PATH + "/../resources/mapper")); // 设置mapperXml生成路径
                })
                .templateConfig(builder -> {
                    builder.disable(TemplateType.CONTROLLER);
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板

                .strategyConfig(builder -> {
                    builder.likeTable(new LikeTable(DB_TABLE_NAME)); // 需要生成实体类的表名
                    builder.entityBuilder()
//                            .superClass(BaseEntity.class) // 设置父类
                            .disableSerialVersionUID()
//                            .enableChainModel() // 开启驼峰转连字符
                            .enableLombok()
                            .enableFileOverride() // 开启文件覆盖
                            .enableRemoveIsPrefix() //开启 Boolean 类型字段移除 is 前缀
                            .enableTableFieldAnnotation()// 开启生成实体时生成字段注解
                            .naming(NamingStrategy.underline_to_camel)// 数据库表映射到实体的命名策略
                            .columnNaming(NamingStrategy.underline_to_camel)// 数据库表字段映射到实体的命名策略   underline_to_camel 下划线 转驼峰
                            .addTableFills(new Column("ccreate_time", FieldFill.INSERT),// 这些都是通用字段
                                    new Column("ccreate_at", FieldFill.INSERT),
//                                    new Column("delete_flag", FieldFill.INSERT),
                                    new Column("cupdate_time", FieldFill.INSERT_UPDATE),
                                    new Column("cupdate_at", FieldFill.INSERT_UPDATE)
                            )
                            .enableColumnConstant()
                            .idType(IdType.AUTO)
                            .formatFileName("%s")
                            .build();
                    builder.serviceBuilder().formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .build();
                    builder.mapperBuilder().formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            .build();

                })
                .execute();
    }
}
